﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ERP.Manager.DataEntity;
using ERP.Manager.BusinessServices;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class ECS_sellstockController : Controller
    {
        private readonly ECS_goodsinfoService ecs_goodsinfoservice;
        private readonly ECS_sellstockinfoService ecs_sellstockinfoservice;
        public ECS_sellstockController() { }
        public ECS_sellstockController(ECS_goodsinfoService ecs_goodsinfoservice, ECS_sellstockinfoService ecs_sellstockinfoservice)
        {
            this.ecs_goodsinfoservice = ecs_goodsinfoservice;
            this.ecs_sellstockinfoservice = ecs_sellstockinfoservice;
        }
        #region Ajax方法添加
        public string Ajax_add_ecs_sellstock(string goods_name, string goods_id, string goods_sn, string goods_type, string brand_id, string order_number, string Delivery_number, string consignment_mode, string consignment_web, string attr_values, string sell_number, string sell_price, string remark)
        {
            try
            {
                ECS_sellstockinfo ecs_sell = new ECS_sellstockinfo();
                ecs_sell.sell_goods_name = goods_name.Trim();
                ecs_sell.sell_goods_id = int.Parse(goods_id);
                ecs_sell.sell_goods_sn = goods_sn.Trim();
                ecs_sell.sell_goods_type.cat_id = int.Parse(goods_type);
                ecs_sell.sell_brand_id = int.Parse(brand_id);
                ecs_sell.sell_order_number = order_number.Trim();
                ecs_sell.sell_Delivery_number = Delivery_number.Trim();
                ecs_sell.sell_consignment_mode = consignment_mode.Trim();
                ecs_sell.sell_consignment_web = consignment_web.Trim();
                ecs_sell.sell_attr_values = attr_values.Trim();
                ecs_sell.sell_number = int.Parse(sell_number);
                ecs_sell.sell_price = decimal.Parse(sell_price);
                ecs_sell.sell_end_time = DateTime.Now.AddDays(0);
                ecs_sell.sell_remark = remark.Trim();
                int rowocunt = ecs_sellstockinfoservice.add_ecs_sellstockinfo(ecs_sell);
                if (rowocunt > 0)
                {
                    int rowcount = ecs_goodsinfoservice.update_ecs_goods_number(ecs_sell.sell_goods_id, ecs_sell.sell_number);
                    if (rowocunt > 0)
                    {
                        return "success";
                    }
                    else
                    {
                        return "error";
                    }
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        #region 显示商品发货信息页面
        public ActionResult ShowECS_sellstock(int? page)
        {
            int pagecount = page ?? 1;
            List<ECS_sellstockinfo> ecs_sellstock = ecs_sellstockinfoservice.all_ecs_sellstockinfo_page(pagecount);
            ViewData["ecs_sellstock"] = ecs_sellstock;
            int rowcount = ecs_sellstockinfoservice.all_ecs_sellstockinfo_page_count();
            ViewData["ecs_sellstock_pagecount"] = rowcount;
            return View();
        }
        #endregion

        #region Ajax请求得到搜索出来的发货商品信息
        public string Ajax_ecs_sellstock(int? page, string sell_goods_name, string begin_time, string end_time)
        {
            int pagecount = page ?? 1;
            List<ECS_sellstockinfo> list = null;
            string html = "";
            list = ecs_sellstockinfoservice.all_ecs_sellstockinfo_page_and_sell_end_time_page(pagecount, sell_goods_name.Trim(), begin_time, end_time);
            if (list.Count == 0)
            {
                html += "<table id=\"Tableuserinfo\" border=\"1px\" width=\"100%\" cellpadding=\"0px\" cellspacing=\"0px\" style=\"border-left: 0px; border-right: 0px;\">";
                html += "<tr style=\"height: 22px;\">";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');border: 0px;\">订单编号</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');border: 0px;\">商品名称</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">发货方式</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">商品属性</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">发货网站</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">发货时间</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');border: 0px;\">发货数量</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');  border: 0px;\">总价</td>";
                html += "</tr>";
                html += "<tr onmouseover=\"onTrover(this);\" onmouseout=\"onTrout(this);\"><td colspan=\"8\" align=\"center\" style=\"height: 30px;\"><font color=\"red\">未搜索到相关的发货商品信息！！！</font></td></tr>";
                html += "</table>";
            }
            else
            {
                html += "<table id=\"Tableuserinfo\" border=\"1px\" width=\"100%\" cellpadding=\"0px\" cellspacing=\"0px\" style=\"border-left: 0px; border-right: 0px;\">";
                html += "<tr style=\"height: 22px;\">";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');border: 0px;\">订单编号</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');border: 0px;\">商品名称</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">发货编号</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">商品属性</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">发货网站</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif'); border: 0px;\">发货时间</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');border: 0px;\">发货数量</td>";
                html += "<td style=\"background-image: url('../../Content/ModeImages/bg2.gif');  border: 0px;\">总价</td>";
                html += "</tr>";
                foreach (var item in list)
                {
                    html += "<tr style=\"height: 25px;\" onmouseover=\"onTrover(this);\" onmouseout=\"onTrout(this);\">";
                    html += "<td>" + item.sell_order_number + "</td>";
                    html += "<td>" + item.sell_goods_name + "</td>";
                    html += "<td>" + item.sell_Delivery_number + "</td>";
                    html += "<td style='width:40%;'>" + item.sell_attr_values + "</td>";
                    html += "<td>" + item.sell_consignment_web + "</td>";
                    html += "<td>" + item.sell_end_time.ToString("yyyy-MM-dd HH:mm") + "</td>";
                    html += "<td>" + item.sell_number + "</td>";
                    html += "<td style='color: Red;'>&yen;<font face='华文新魏'>" + item.sell_price + "</font>元</td>";
                    html += "</tr>";
                }
                html += "</table>";
                //得到所有数据的总数；
                int list_count = ecs_sellstockinfoservice.all_ecs_sellstockinfo_page_and_sell_end_time_count(sell_goods_name, begin_time, end_time);
                //获得总页码数
                int pagesize = (int)Math.Ceiling(list_count / (double)11);
                html += "<br /><table border='0px'><tr>";
                if (pagecount == 1)
                {
                    html += "<td>总共有:&nbsp;<font color='red'>" + list_count + "</font>&nbsp;条数据&nbsp;&nbsp;</td><td>当前第" + pagecount + "/" + pagesize + "页";
                    html += "&nbsp;&nbsp;&nbsp;<a href='javascript:'>首页</a>&nbsp;&nbsp;<a href='javascript:'>上一页</a>";
                }
                else
                {
                    html += "<td>总共有:&nbsp;<font color='red'>" + list_count + "</font>&nbsp;条数据&nbsp;&nbsp;</td><td>当前第" + pagecount + "/" + pagesize + "页";
                    html += "&nbsp;&nbsp;&nbsp;<a href='javascript:searchsell_end_time(" + 1 + ")'>首页</a>&nbsp;&nbsp;<a href='javascript:searchsell_end_time(" + (pagecount - 1) + ");'>上一页</a>";
                }
                if (pagecount == pagesize)
                {
                    html += "&nbsp;&nbsp;<a href='javascript:'>下一页</a>&nbsp;&nbsp;";
                    html += "<a href='javascript:'>尾页</a></td>";
                }
                else
                {
                    html += "&nbsp;&nbsp;<a href='javascript:searchsell_end_time(" + (pagecount + 1) + ");'>下一页</a>&nbsp;&nbsp;";
                    html += "<a href='javascript:searchsell_end_time(" + pagesize + ");'>尾页</a></td>";
                }
                html += "</tr>";
                html += "</table>";
            }
            return html;
        }
        #endregion

        #region 验证商品的发货编号是否在数据库中存在
        public string Ajax_validator_sell_Delivery_number(string sell_delivery_number)
        {
            string new_sell_delivery_number = sell_delivery_number.Trim();
            try
            {
                List<ECS_sellstockinfo> ecs_sell = ecs_sellstockinfoservice.Ajax_validator_sell_Delivery_number(new_sell_delivery_number);
                if (ecs_sell.Count > 0)
                {
                    return "error";
                }
                else
                {
                    return "success";
                }
            }
            catch (Exception)
            {
                return "error";
            }
        }
        #endregion
    }
}
